/*******************************************************************
 *
 * Copyright (C), 2021-2022, Letstrong, All rights reserved.
 *
 * Author      : 
 * Create Time : 2020-12-14
 * Version     : v0.1
 * Description : 
 *******************************************************************/
#ifndef _OTA_PROTOCOL_H_
#define _OTA_PROTOCOL_H_

#include "stdint.h"
#include "rwip_config.h"

#if (BLE_OTA_SERVER)

#define NUM_OF_NTF_PKT_MAX_NUM          12
#define NUM_OF_NTF_PKT_NUM_NOT_SET      0xFFFF

#define OTA_IMAGE_TYPE_BOOTLOADER		2
#define OTA_IMAGE_TYPE_APPLICATION		4
#define OTA_IMAGE_TYPE_UNVALID			0xFF

#define BREAK_UINT32( var, ByteNum ) \
          (uint8_t)((uint32_t)(((var) >>((ByteNum) * 8)) & 0x00FF))

enum{
	OTA_CODE_START_OTA				= 1,		/**< Value of the Op code field for 'Start OTA' command.*/
	OTA_CODE_RECEIVE_INIT			= 2,		/**< Value of the Op code field for 'Initialize OTA parameters' command.*/
	OTA_CODE_RECEIVE_FW				= 3,		/**< Value of the Op code field for 'Receive firmware image' command.*/
	OTA_CODE_VALIDATE				= 4,		/**< Value of the Op code field for 'Validate firmware' command.*/
	OTA_CODE_ACTIVATE_N_RESET		= 5,		/**< Value of the Op code field for 'Activate & Reset' command.*/
	OTA_CODE_SYS_RESET				= 6,		/**< Value of the Op code field for 'Reset System' command.*/
	OTA_CODE_IMAGE_SIZE_REQ			= 7,		/**< Value of the Op code field for 'Report received image size' command.*/
	OTA_CODE_PKT_RCPT_NOTIF_REQ		= 8,		/**< Value of the Op code field for 'Request packet receipt notification.*/
	                                            /**< Value of the Op code field for 'Response.*/
	OTA_CODE_RESPONSE				= 16,		/**< Value of the Op code field for 'Packets Receipt Notification'.*/
	OTA_CODE_PKT_RCPT_NOTIF			= 17,		
};

/**@brief Packet type enumeration.
 */
typedef enum
{
    PKT_TYPE_INVALID,                                                                                /**< Invalid packet type. Used for initialization purpose.*/
    PKT_TYPE_START,                                                                                  /**< Start packet.*/
    PKT_TYPE_INIT,                                                                                   /**< Init packet.*/
    PKT_TYPE_FIRMWARE_DATA                                                                           /**< Firmware data packet.*/
} PktType_t;

/**@brief   OTA Response value type.
 */
typedef enum
{
    OTA_RESP_VAL_SUCCESS = 1,                                       /**< Success.*/
    OTA_RESP_VAL_INVALID_STATE,                                     /**< Invalid state.*/
    OTA_RESP_VAL_NOT_SUPPORTED,                                     /**< Operation not supported.*/
    OTA_RESP_VAL_DATA_SIZE,                                         /**< Data size exceeds limit.*/
    OTA_RESP_VAL_CRC_ERROR,                                         /**< CRC Error.*/
    OTA_RESP_VAL_OPER_FAILED                                        /**< Operation failed.*/
} OtaRespVal_t;

typedef struct{
	uint8_t imageType;			//Image type: 0x00,Bootloader;  0x01,Application
	uint32_t blImageSize;		//Bootloader image size
	uint32_t appImageSize;		//Application image size
	uint16_t notifyPacketsNum;	//number of notify packets
	uint8_t initPacket[32];		//Init Packet
	uint8_t initPacketLen;		//Init Packet length
	uint16_t imageCRC16;		//Image CRC16 CCITT
    uint32_t imageVersion;
	
	uint32_t blImageRecSize;	//Bootloader receive image size
	uint32_t appImageRecSize;	//Application receive image size
	uint16_t numOfNotifyRecPackets;		//number of notify receive packets
	PktType_t packetType;		//packet type
} OTA_INFO_t;


extern void otaProtocol_ProcessControlEvt(uint8_t *dat, uint8_t len);
extern void otaProtocol_ProcessPacketDataEvt(uint8_t *dat, uint8_t len);

#endif /* #if (BLE_OADS_SERVER) */


#endif /*  _OTA_PROTOCOL_H_ */

